package Test;

import java.util.List;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.demo.entity.Student;
import com.demo.entity.StudentInfo;
import com.demo.service.StudentService;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/spring-*.xml")
public class StudentTest {
	@Autowired
	private StudentService studentService;
	
	//增加学生 
	@Test
	public void addStudent() throws Exception {
		Student student = new Student();
		student.setId("s5");
		student.setAge(23);
		student.setClassid("c1");
		student.setSex(0);
		student.setName("小王");
		int result = studentService.addStudent(student);
		System.out.println(result);//1 success  exception error 主键重复
	}
	//根据id号删除学生
	@Test
	public void delStudent() throws Exception{
		System.out.println(studentService.deleteStudentById("s4"));//1 success 0 error
	}
	
	//更新除了id外学生的信息
	@Test
	public void updateStudent() throws Exception{
		Student student = studentService.selectStudentById("s1");
		System.out.println("before update");
		System.out.println(student);
		//Student [id=s1, name=王五, age=21, sex=1, classid=c1]
		student.setAge(22);
		student.setClassid("c2");
		student.setName("王武");
		student.setSex(0);
		System.out.println(studentService.updateStudent(student));
		System.out.println("after udpate");
		Student studentu = studentService.selectStudentById("s1");
		System.out.println(studentu);
	}
	//根据id号查询学生
	@Test
	public void selectStudentById() throws Exception {
		Student selectStudentById = studentService.selectStudentById("s1");
		System.out.println(selectStudentById);
	}
	//查询所有学生
	@Test
	public void selectAllStudent() throws Exception{
		List<Student> selectAllStudent = studentService.selectAllStudent();
		for (Student student : selectAllStudent) {
			System.out.println(student);
		}
	}
	//查询所有学生信息和班级信息
	@Test
	public void selectAllStudentInfo() throws Exception{
		List<Student> selectAllStudent = studentService.selectAllStudentInfo();
		for (Student student : selectAllStudent) {
			System.out.println(student);
		}
	}
	@Test 
	public void selectAllStudentInfoE() throws Exception{
		List<StudentInfo> selectAllStudentInfoE = studentService.selectAllStudentInfoE();
		for (StudentInfo studentInfo : selectAllStudentInfoE) {
			System.out.println(studentInfo);
		}
	}
}
